package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AnswerTobeModel;
import com.banshuibao.bsb_android.model.TobeModel;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.QuickDoubleClickable;
import com.banshuibao.bsb_android.util.TimeUtil;

public class AnswerTobeActivity extends BaseActivity implements View.OnClickListener {

    private ImageView headerImage;
    private TextView name;
    private TextView content;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView3;
    private TextView date;
    private Button qiang;
    private Button qiang_an;
    private String[] split;
    private String uuid;
    private String headImg;
    private String name1;
    private String content1;
    private long createDate;
    private String imgs;
    private LinearLayout lin;
    private TextView warning;
    private TextView qPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_tobe);
        initView();
        getData();
    }

    private void getData() {
        TobeModel.DataBean item = (TobeModel.DataBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            uuid = item.getUuid();
            headImg = item.getHeadImg();
            name1 = item.getName();
            content1 = item.getContent();
            String status = item.getStatus();
            createDate = item.getCreateDate();
            if (!TextUtils.isEmpty(headImg)) {
                ImageUtil.loadImageHead(this, headImg, headerImage);
            }
            name.setText(name1);
            content.setText(content1);
            String time = null;
            if (createDate != 0) {
                time = TimeUtil.format(createDate);
            } else {
                time = "";
            }
            date.setText(time);
            if (status.equals("0")) {
                qiang.setVisibility(View.VISIBLE);
                qiang_an.setVisibility(View.GONE);
                qPrice.setTextColor(context.getResources().getColor(R.color.price));
            } else if (status.equals("1")) {
                qiang.setVisibility(View.GONE);
                qiang_an.setVisibility(View.VISIBLE);
                qPrice.setTextColor(context.getResources().getColor(R.color.text_default));
            }
            imgs = item.getImgs();
            if (!TextUtils.isEmpty(imgs)) {
                split = imgs.split(",");
                if (split.length > 0) {
                    if ("null".equals(split[0]))
                        return;
                    if (split.length == 1) {
                        ImageUtil.loadImage(this, split[0], imageView1);
                        imageView2.setVisibility(View.GONE);
                        imageView3.setVisibility(View.GONE);
                    } else if (split.length == 2) {
                        ImageUtil.loadImage(this, split[0], imageView1);
                        ImageUtil.loadImage(this, split[1], imageView2);
                        imageView3.setVisibility(View.GONE);
                    } else if (split.length == 3) {
                        ImageUtil.loadImage(this, split[0], imageView1);
                        ImageUtil.loadImage(this, split[1], imageView2);
                        ImageUtil.loadImage(this, split[2], imageView3);
                    }
                }
            } else {
                imageView1.setVisibility(View.GONE);
                imageView2.setVisibility(View.GONE);
                imageView3.setVisibility(View.GONE);
                lin.setVisibility(View.GONE);
            }
            warning.setText(getResources().getString(R.string.tobe_tag)+TimeUtil.format(item.getEndDate()));
            qPrice.setText(PriceUtil.FormatDecimal(item.getPrice())+"元");
        }
    }

    private void initView() {
        setTitleText("咨询详情");
        headerImage = (ImageView) findViewById(R.id.header_iamge);
        name = (TextView) findViewById(R.id.name);
        content = (TextView) findViewById(R.id.content);
        imageView1 = (ImageView) findViewById(R.id.image_one);
        imageView2 = (ImageView) findViewById(R.id.image_two);
        imageView3 = (ImageView) findViewById(R.id.image_three);
        lin = (LinearLayout) findViewById(R.id.liner);
        date = (TextView) findViewById(R.id.date);
        qiang = (Button) findViewById(R.id.btn);
        qiang_an = (Button) findViewById(R.id.btn_an);
        warning = findViewById(R.id.tv);
        qPrice = findViewById(R.id.qPrice);
        imageView1.setOnClickListener(this);
        imageView2.setOnClickListener(this);
        imageView3.setOnClickListener(this);
        qiang.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(AnswerTobeActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(AnswerTobeActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(AnswerTobeActivity.this, split, 2);
                break;
            case R.id.btn:
                if (QuickDoubleClickable.isDoubleClick())
                    return;
                shouPopup();
                break;
        }
    }

    private void shouPopup() {
        View view = View.inflate(this, R.layout.respond_question_popup, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        TextView yes = (TextView) view.findViewById(R.id.yes);
        TextView no = (TextView) view.findViewById(R.id.no);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog();
                setPost();
                popupWindow.dismiss();
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        View parent = View.inflate(AnswerTobeActivity.this, R.layout.activity_answer_tobe, null);
        popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    private void setPost() {
        AnswerTobeModel.body body = new AnswerTobeModel.body();
        if (!TextUtils.isEmpty(uuid)) {
            body.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.Q_CONSULTTATION, body, new HttpResponse(this,AnswerTobeModel.class) {
            @Override
            public void onResult(Object obj) {
                AnswerTobeModel s = (AnswerTobeModel) obj;
                String data = s.getData();
                cancelProgressDialog();
                Intent intent = new Intent(AnswerTobeActivity.this, CommitAnswerActivity.class);
                intent.putExtra("uuid",data);
                intent.putExtra("flag",1);
                intent.putExtra("headImage",headImg);
                intent.putExtra("name",name1);
                intent.putExtra("content",content1);
                intent.putExtra("time",createDate);
                intent.putExtra("imgs",imgs);
                startActivity(intent);
                finish();
//                LogUtil.e(s);
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }
}
